﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using ReflectionStudio.Core.Events;
using INTEX.DataBase;
using INTEX.DataBase.Tools;
namespace WPFExcelReport
{

    public class JLTVK03HLData : BaseFunction
    {
        public bool checkVK07 = true;
        SETINTEXDB INTEXDB = new SETINTEXDB();
        INTEXDBC INTEXDBC;
        public JLTVK03HLData()
            : base()
        {

        }
        public JLTVK03HLData(OracleConnection _myConnection, IntexReptServBLClient _proxy, string userID = "        ")
            : base(_myConnection, _proxy, userID)
        {
            INTEXDBC = new INTEXDBC(OracleDB, proxy);
        }
        public JLTVK03HLData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {

        }
        public DataSet GetData(DataRow dwInput)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();

            try
            {
                DataRow dwTemp;
                SResult = INTEXDBC.QueryVKKOPF(dwInput, 1);
                SReturn.Merge(SResult);
                SResult = GetVKKTXT(dwInput);
                SReturn.Merge(SResult);
                dwTemp = INTEXDB.VKPOS.NewRow();
                dwTemp["VKPMANDANT"] = dwInput["VKKMANDANT"];
                dwTemp["VKPAUFKRS"] = dwInput["VKKAUFKRS"];
                dwTemp["VKPAUFLFD"] = dwInput["VKKAUFLFD"];
                dwTemp["VKPNEUDAT"] = dwInput["VKKNEUDAT"];
                SResult = INTEXDBC.QueryVKPOS(dwTemp, 4);
                SReturn.Merge(SResult);
                if (SResult.Tables[0].Rows.Count == 0)
                    return SReturn;
                DataRow dwVKP = SResult.Tables[0].Rows[0];
                SResult = INTEXDBC.QueryVAART(dwVKP, 3);
                SReturn.Merge(SResult);
                SResult = INTEXDBC.QueryVAFARBEN(dwVKP, 3);
                SReturn.Merge(SResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ABPOS + ex.StackTrace, ex);
            }
            finally
            {
                EventDispatcher.Instance.RaiseStatus("查询完成", StatusEventType.StopProgress);
            }
            return SReturn;
        }
        public string Today
        {
            get
            {
                DateTime now = System.DateTime.Now;
                string year = now.Year.ToString();
                string m =Right("00" +now.Month.ToString(),2);
                string day = Right("00" +now.Day.ToString(),2);
                return year + m + day;
            }
        } 
        public DataSet GetVKKTXT(DataRow dwInput)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();            
            try
            {
                string KRS = dwInput["VKKAUFKRS"].ToString();
                string LFD = dwInput["VKKAUFLFD"].ToString();
                string VKPAUF = KRS + LFD;
                SResult = GetTXT(VKPAUF);
                string TXTISIND = ""; 
                DataRow dwTXTKON = null;
                DataTable tbTXTKON = setTXT.TXTKON.Copy();
                string TXTKPOS = "";
                int pos = 1;
                Dictionary<string, int> txtID = new Dictionary<string, int>();
                string processed = "";
                foreach (DataRow dwTXT in SResult.Tables["TXTKON"].Rows)
                {
                    TXTISIND = dwTXT["TXTISIND"].ToString();
                    TXTKPOS = dwTXT["TXTKPOS"].ToString();
                    if (!processed.Contains(TXTISIND + ";"))
                    {
                        processed += TXTISIND + ";";
                        dwTXTKON = tbTXTKON.NewRow();
                        tbTXTKON.Rows.Add(dwTXTKON);
                        dwTXTKON["TXTIPIND"] = TXTISIND;
                    }
                    else
                    {
                        var query = from DataRow dw in tbTXTKON.AsEnumerable()
                                    where dw["TXTIPIND"].ToString() == TXTISIND
                                    select dw;
                        foreach (DataRow dw in query)
                        {
                            dwTXTKON = dw;
                        }
                    }

                    if (txtID.ContainsKey(TXTISIND))
                    {
                        pos = txtID[TXTISIND];
                        pos++;
                        txtID[TXTISIND] = pos;
                    }
                    else
                    {
                        pos = 1;
                        txtID.Add(TXTISIND, pos);
                    }
                    TXTKPOS = Right("000" + pos.ToString() + "0", 3);

                    if (tbTXTKON.Columns.Contains("TXT" + TXTKPOS))
                    {
                        dwTXTKON["TXT" + TXTKPOS] = dwTXT["TXTKTEXT"];
                    }

                }
                SReturn.Merge(tbTXTKON);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        } 
        public string GetMemberV9(string company = "DST")
        {
            string memberv9 = "";
            try
            {
                OracleCommand or1;
                or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT HRMANDANT,HRBENID,HRPASSW,HRSPRKZ,HRVNAME,HRNNAME"
                + " FROM HRSTAMM "
                + "WHERE HRBGRP='V9' AND HRBERST=9";
                setResult = Connect2Proxy(or1);
                foreach (DataRow dwTemp in setResult.Tables[0].Rows)
                {
                    memberv9 += dwTemp["HRBENID"].ToString().Trim() + ";";
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return memberv9;
        }
        public string GetAdministration(string company = "DST")
        {
            string Administration = "";
            try
            {
                OracleCommand or1;
                or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT HRMANDANT,HRBENID,HRPASSW,HRSPRKZ,HRVNAME,HRNNAME"
                + " FROM HRSTAMM "
                + "WHERE HRSACHG like '%ADMINISTRATION%' AND HRBERST=9";
                setResult = Connect2Proxy(or1);
                foreach (DataRow dwTemp in setResult.Tables[0].Rows)
                {
                    Administration += dwTemp["HRBENID"].ToString().Trim() + ";";
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return Administration;
        }
        public override DataSet LogIn(string MANDANT, string UsingID, System.Security.SecureString sPassword)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult;
            DataTable tableLocal = null;
            OracleCommand or1;
            try
            {
                IntPtr p = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(sPassword);
                string Password = System.Runtime.InteropServices.Marshal.PtrToStringBSTR(p);
                UsingID = UsingID.Trim().ToUpper();
                Password = Password.Trim().ToUpper();
                DataRow dwTemp = INTEXDB.HRSTAMM.NewRow();
                dwTemp["HRMANDANT"] = MANDANT;
                dwTemp["HRBENID"] = Left(UsingID + "                    ", 8);
                dwTemp["HRDFAX"] =Left(Password+"                    ",20);
                SResult = INTEXDBC.QueryHRSTAMM(dwTemp, 1);
                SReturn.Merge(SResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); ;
            }
            return SReturn;
        }
    }
}